#include "filewatch.h"

#include <QFileSystemWatcher>
#include <QDebug>
#include <QDir>

FileWatch::FileWatch(const QString filePath, QObject *parent)
    : QObject(parent)
    , m_FilePath(filePath)
{
    m_fileFilter << "*.txt" << "*.md";

    m_watchFilesInfoHistory = QDir(m_FilePath).entryInfoList(m_fileFilter,
                                                             QDir::Files|QDir::Readable,
                                                             QDir::Time);
    initWatch();
}

QStringList FileWatch::getFileList()
{
    QStringList fileList;

    foreach (QFileInfo info, m_watchFilesInfoHistory) {

        fileList.append(info.absoluteFilePath());
    }
    return fileList;
}

void FileWatch::sltFileChanged(const QString &path)
{
    if(m_pWatch->files().contains(path))
    {
        qDebug() << "修改了文件:" << path;
    }else{
        qDebug() << "删除了文件(也可能是重命名):" << path;
    }
}

void FileWatch::sltDirectoryChanged(const QString &path)
{
 checkFileIsRename(m_watchFilesInfoHistory);
}

void FileWatch::initWatch()
{
    m_pWatch = new QFileSystemWatcher(this);

   qDebug() << "添加监视:"<<  m_pWatch->addPath(m_FilePath);

//这里不监视文件，如果文件被修改了无法判断
//qDebug() << "添加文件监视:" << m_pWatch->addPaths(getFilesFromPath(m_FilePath));

    connect(m_pWatch, &QFileSystemWatcher::fileChanged,
            this, &FileWatch::sltFileChanged);
    connect(m_pWatch, &QFileSystemWatcher::directoryChanged,
            this, &FileWatch::sltDirectoryChanged);

}

QStringList FileWatch::getFilesFromPath(const QString filePath)
{
    QDir dir(filePath);

    QFileInfoList fileInfoList = dir.entryInfoList(QStringList() << "*txt" << "*md",
                                           QDir::Files|QDir::Readable,
                                           QDir::Time);

    QStringList fileList;

    foreach (QFileInfo file, fileInfoList) {

        fileList.append(file.absoluteFilePath());

    }
    return fileList;

}



void FileWatch::checkFileIsRename(QFileInfoList &fileHistory)
{
    QDir dir(m_FilePath);

    QFileInfoList  newInfoFileList = QDir(m_FilePath).entryInfoList(m_fileFilter,
                                                             QDir::Files|QDir::Readable,
                                                             QDir::Time);


    QFileInfoList oldNotHave;
    QFileInfoList newNotHave;


    QStringList oldNotHaveList;
    QStringList newNotHaveList;

    foreach (QFileInfo info, newInfoFileList) {

        if(!fileHistory.contains(info))
        {
            oldNotHave.append(info);
            oldNotHaveList.append(info.absoluteFilePath());

        }
    }


    foreach (QFileInfo info, fileHistory) {

        if(!newInfoFileList.contains(info))
        {
            newNotHave.append(info);
            newNotHaveList.append(info.absoluteFilePath());
        }
    }




    if(newInfoFileList.size() > fileHistory.size())
    {//新增
        qDebug() << "新增了文件：" << oldNotHaveList;

        emit sigFileChange(oldNotHaveList,addFiles);
        qDebug() << "添加新监视:"<< m_pWatch->addPaths(getFilesFromPath(m_FilePath));

    }
    else if(newInfoFileList.size() < fileHistory.size())
    {//删除
        emit sigFileChange(newNotHaveList,delFiles);
        qDebug() << "删除了文件：" << newNotHaveList;

    }else if(newInfoFileList.size() == fileHistory.size())
    {//重命名
        if(newNotHave.isEmpty() ||oldNotHave.isEmpty() )
        {
            //qWarning() << "未检测到文件发生改变";
        }else
        {
            QStringList list;
            list.append(newNotHaveList.first());
            list.append(oldNotHaveList.first());
            emit sigFileChange(list,renameFiles);
            qDebug() << "文件重命名: " << newNotHave.first().absoluteFilePath() << "---> " << oldNotHave.first().absoluteFilePath();

        }
    }

    fileHistory  = newInfoFileList;

}


